//
//  ZCItemListVC.m
//  zctx
//
//  Created by 郭亚京 on 15/10/9.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCItemListVC.h"
#import "ZCItenCell.h"
#import "ZCItemDetailVC.h"
@interface ZCItemListVC ()<UITableViewDelegate, UITableViewDataSource,CelldetailsDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *selectindexPath;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ZCItemListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationItems];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    self.navigationItem.title = @"选择检测项目";
    
    //添加tableview
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,100,0));
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCItenCell" bundle:nil] forCellReuseIdentifier:@"itencell"];
    [self initbottomButton];
    [self requestMainList];
    
}
#pragma mark 确认按钮
-(void)initbottomButton{
    UIButton *okButton = [[UIButton alloc]init];
    [self.view addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(50);
        make.right.equalTo(self.view).with.offset(-50);
        make.height.mas_equalTo(@40);
        make.bottom.equalTo(self.view).with.offset(-30);
    }];
    [okButton addTarget:self action:@selector(ClickokButton) forControlEvents:UIControlEventTouchUpInside];
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    [okButton setBackgroundImage:[UIImage imageNamed:@"raise_confirm_bg"] forState:UIControlStateNormal];
}
#pragma mark - request

- (void)requestMainList {
    ProgressHUDStr;
    [NetworkManager requestFoundingGetPackagessuccess:^(NSArray *array) {
        ProgressDismiss;
        self.dataArray = array.mutableCopy;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

#pragma mark - button

-(void)setNavigationItems {
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

- (void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource & Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [self.dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"itencell";
    ZCItenCell *cell =(ZCItenCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[ZCItenCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate =self;
    cell.indexPath = indexPath;
    ZCItemModel *model = self.dataArray[indexPath.section];
    cell.nameLable.text = model.name;
    NSInteger money = [[NSString stringWithFormat:@"%@",model.price] integerValue];
    cell.moneyLable.text =[NSString stringWithFormat:@"%ld元",money/100];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    self.selectindexPath = indexPath;
}

#pragma mark 点击了详情
-(void)celldetailsPath:(NSIndexPath *)indexPath{

    ZCItemModel *model = self.dataArray[indexPath.section];
    ZCItemDetailVC *zcvc = [[ZCItemDetailVC alloc]init];
    zcvc.hidesBottomBarWhenPushed = YES;
    zcvc.model = model;
    [self.navigationController pushViewController:zcvc animated:YES];;
}

#pragma mark 
-(void)ClickokButton{
    
    if (self.selectindexPath) {
        ZCItemModel *model =[self.dataArray objectAtIndex:self.selectindexPath.section];
        NSString *pid = [NSString stringWithFormat:@"%@",model.id];
        NSString *pname = [NSString stringWithFormat:@"%@",model.name];
        NSInteger  price = [[NSString stringWithFormat:@"%@",model.price]integerValue];
        NSString * moneyprice = [NSString stringWithFormat:@"%ld",price/100];
        NSLog(@"pid = %@ , model = %@",pid,pname);
        self.cellselectItemList(pid,pname,moneyprice);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - instance

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

@end
